package com.example.service.impl;

import com.example.entity.*;
import com.example.mappers.*;
import com.example.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class EquipmentSrviceImpl implements EquipmentService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private ElementMapper elementMapper;
    @Autowired
    private Equipment_ElementMapper equipment_elementMapper;
    @Autowired
    private Element_MaintainMapper element_maintainMapper;
    @Autowired
    private Element_ReplaceMapper element_replaceMapper;
    @Autowired
    private Element_RepairMapper element_repairMapper;
    @Autowired
    private MalfunctionMapper malfunctionMapper;

    @Override
    public String AddCategory(Category category) {
        if (categoryMapper.GetCategoryByCategoryName(category.getCategory_name()) == null)
        {
            categoryMapper.AddCategory(category);
            return "{'type_code':'category'}";
        }
        return "{'type_code':'error'}";
    }

    @Override
    public List<Category> RequestCategory(Integer army_id) {
        return categoryMapper.GetCategoryByArmyId(army_id);
    }

    @Override
    public String AddEquipment(Equipment equipment) {
        if (equipmentMapper.GetEquipmentByEquipmentName(equipment.getEquipment_name()) == null)
        {
            equipmentMapper.AddEquipment(equipment);
            return "{'type_code':'equipment'}";
        }
        return "{'type_code':'error'}";
    }

    @Override
    public List<Equipment> RequestEquipment(Integer army_id) {
        return equipmentMapper.GetEquipmentByArmyId(army_id);
    }

    @Override
    public String AddElement(Element element) {
        if (elementMapper.GetElementByName(element.getElement_name()) == null)
        {
            elementMapper.AddElement(element);
            return "{'type_code':'element'}";
        }
        return "{'type_code':'error'}";
    }

    @Override
    public List<Element> RequestElement(Integer category_id) {
        return elementMapper.GetElementByCategoryId(category_id);
    }

    @Override
    public String AddEquipmentElement(Integer equipment_id,Integer element_id) {
        if (equipment_elementMapper.GetEquipmentElement(equipment_id,element_id) == null)
        {
            equipment_elementMapper.AddEquipmentElement(equipment_id,element_id);
            return "{'type_code':'equipment_element'}";
        }
        return "{'type_code':'error'}";
    }

    @Override
    public Equipment_Element RequestEquipmentElement(Integer equipment_id, Integer element_id) {
        return equipment_elementMapper.GetEquipmentElement(equipment_id,element_id);
    }

    @Override
    public List<Equipment_Element> RequestEquipElementByEquipId(Integer equipment_id) {
        return equipment_elementMapper.GetEquipmentElements(equipment_id);
    }

    @Override
    public String AddMaintainMsg(Element_Maintain element_maintain) {
        try {
            element_maintainMapper.AddElementMaintain(element_maintain);
            return "{'type_code':'element_maintain'}";
        }
        catch (Exception p)
        {
            return "{'type_code':'error'}";
        }
    }

    @Override
    public List<Element_Maintain> RequestMaintain(Integer element_repair_id) {
        return element_maintainMapper.GetElementMaintainByElementId(element_repair_id);
    }

    @Override
    public String AddReplace(Element_Replace element_replace) {
        try {
            element_replaceMapper.AddElementReplace(element_replace);
            return "{'type_code':'element_replace'}";
        }
        catch (Exception p)
        {
            return "{'type_code':'error'}";
        }
    }

    @Override
    public List<Element_Replace> RequestReplace(Integer element_repair_id) {
        return element_replaceMapper.GetElementReplaceByElementId(element_repair_id);
    }

    @Override
    public String AddRepair(Element_Repair element_repair) {
        try {
            element_repairMapper.AddElementRepair(element_repair);
            return "{'type_code':'element_replace'}";
        }
        catch (Exception p)
        {
            return "{'type_code':'error'}";
        }
    }

    @Override
    public List<Element_Repair> RequestRepair(Integer element_repair_id) {
        return element_repairMapper.GetElementRepairByElementId(element_repair_id);
    }

    @Override
    public String AddMalfunction(Malfunction malfunction) {
        return null;
    }

    @Override
    public List<Malfunction> RequestMalfunction(Integer element_id) {
        return malfunctionMapper.GetMalfunctionByElementId(element_id);
    }

    @Override
    public String AddReason(Reason reason) {
        return null;
    }

    @Override
    public List<Reason> RequestReason(Integer malfunction_id) {
        return malfunctionMapper.GetMalfunctionReason(malfunction_id);
    }

    @Override
    public String AddSolution(Solution solution) {
        return null;
    }

    @Override
    public List<Solution> RequestSolution(Integer malfunction_id) {
        return malfunctionMapper.GetMalfunctionSolution(malfunction_id);
    }
}
